package com.qqim.common.protocol;

import java.io.Serializable;

/**
 * 网络通信协议
 */
public class Protocol implements Serializable {
    private static final long serialVersionUID = 1L;
    
    // 协议类型常量
    public static final String LOGIN = "LOGIN";
    public static final String LOGIN_SUCCESS = "LOGIN_SUCCESS";
    public static final String LOGIN_FAILED = "LOGIN_FAILED";
    public static final String LOGOUT = "LOGOUT";
    public static final String GET_USER_LIST = "GET_USER_LIST";
    public static final String USER_LIST = "USER_LIST";
    public static final String SEND_MESSAGE = "SEND_MESSAGE";
    public static final String RECEIVE_MESSAGE = "RECEIVE_MESSAGE";
    public static final String SEND_IMAGE = "SEND_IMAGE";
    public static final String RECEIVE_IMAGE = "RECEIVE_IMAGE";
    public static final String USER_ONLINE = "USER_ONLINE";
    public static final String USER_OFFLINE = "USER_OFFLINE";
    public static final String HEARTBEAT = "HEARTBEAT";
    public static final String ERROR = "ERROR";
    
    /** 协议类型 */
    private String type;
    
    /** 发送者ID */
    private String senderId;
    
    /** 接收者ID */
    private String receiverId;
    
    /** 协议数据 */
    private Object data;
    
    /** 时间戳 */
    private long timestamp;
    
    /** 错误信息 */
    private String errorMessage;
    
    public Protocol() {
        this.timestamp = System.currentTimeMillis();
    }
    
    public Protocol(String type) {
        this();
        this.type = type;
    }
    
    public Protocol(String type, Object data) {
        this(type);
        this.data = data;
    }
    
    public Protocol(String type, String senderId, String receiverId, Object data) {
        this(type, data);
        this.senderId = senderId;
        this.receiverId = receiverId;
    }
    
    // 静态工厂方法
    public static Protocol login(String userId, String nickname) {
        Protocol protocol = new Protocol(LOGIN);
        protocol.setSenderId(userId);
        protocol.setData(nickname);
        return protocol;
    }
    
    public static Protocol loginSuccess(String userId) {
        Protocol protocol = new Protocol(LOGIN_SUCCESS);
        protocol.setSenderId(userId);
        return protocol;
    }
    
    public static Protocol loginFailed(String errorMessage) {
        Protocol protocol = new Protocol(LOGIN_FAILED);
        protocol.setErrorMessage(errorMessage);
        return protocol;
    }
    
    public static Protocol logout(String userId) {
        Protocol protocol = new Protocol(LOGOUT);
        protocol.setSenderId(userId);
        return protocol;
    }
    
    public static Protocol getUserList() {
        return new Protocol(GET_USER_LIST);
    }
    
    public static Protocol userList(Object userList) {
        return new Protocol(USER_LIST, userList);
    }
    
    public static Protocol sendMessage(String senderId, String receiverId, Object message) {
        return new Protocol(SEND_MESSAGE, senderId, receiverId, message);
    }
    
    public static Protocol receiveMessage(String senderId, String receiverId, Object message) {
        return new Protocol(RECEIVE_MESSAGE, senderId, receiverId, message);
    }
    
    public static Protocol sendImage(String senderId, String receiverId, Object imageData) {
        return new Protocol(SEND_IMAGE, senderId, receiverId, imageData);
    }
    
    public static Protocol receiveImage(String senderId, String receiverId, Object imageData) {
        return new Protocol(RECEIVE_IMAGE, senderId, receiverId, imageData);
    }
    
    public static Protocol userOnline(String userId) {
        Protocol protocol = new Protocol(USER_ONLINE);
        protocol.setSenderId(userId);
        return protocol;
    }
    
    public static Protocol userOffline(String userId) {
        Protocol protocol = new Protocol(USER_OFFLINE);
        protocol.setSenderId(userId);
        return protocol;
    }
    
    public static Protocol heartbeat(String userId) {
        Protocol protocol = new Protocol(HEARTBEAT);
        protocol.setSenderId(userId);
        return protocol;
    }
    
    public static Protocol error(String errorMessage) {
        Protocol protocol = new Protocol(ERROR);
        protocol.setErrorMessage(errorMessage);
        return protocol;
    }
    
    public String getType() {
        return type;
    }
    
    public void setType(String type) {
        this.type = type;
    }
    
    public String getSenderId() {
        return senderId;
    }
    
    public void setSenderId(String senderId) {
        this.senderId = senderId;
    }
    
    public String getReceiverId() {
        return receiverId;
    }
    
    public void setReceiverId(String receiverId) {
        this.receiverId = receiverId;
    }
    
    public Object getData() {
        return data;
    }
    
    public void setData(Object data) {
        this.data = data;
    }
    
    public long getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
    
    public String getErrorMessage() {
        return errorMessage;
    }
    
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
    
    @Override
    public String toString() {
        return "Protocol{" +
                "type='" + type + '\'' +
                ", senderId='" + senderId + '\'' +
                ", receiverId='" + receiverId + '\'' +
                ", data=" + data +
                ", timestamp=" + timestamp +
                ", errorMessage='" + errorMessage + '\'' +
                '}';
    }
} 